//  Copyright (c) 2010 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System.Web.Mvc;
using DNA.Mvc.Community;
using DNA.Mvc.Controllers;
using System.Collections.Generic;
using System.Linq;

namespace DNA.Mvc.Community.Controllers //DNA.Mvc.Areas.Community.Controllers
{
    public class RankController : Controller
    {
        private IForumContext forumContext;

        public RankController(IForumContext dbContext)
        {
            forumContext = dbContext;
        }

        [SecurityAction("Community", "Manage ranks", "Allows user manage ranks",
    TitleResName = "SA_ManageRanks",
    DescResName = "SA_ManageRanksDesc",
    PermssionSetResName = "SA_Community"
    )]
        [SiteControlPanel(ResBaseName = "forums", ResKey = "Ranks", Order = 2, Group = "Community", ShowInTopSiteOnly = true)]
        public ActionResult List()
        {
            return View(forumContext.Ranks.All().ToList());
        }

        [SecurityAction("Community", "Create rank", "Allows user can create new rank",
            TitleResName = "SA_CreateRank",
            DescResName = "SA_CreateRankDesc",
            PermssionSetResName = "SA_Community"
            )]
        public ActionResult Create()
        {
            return View("Edit");
        }

        [SecurityAction("Community", "Edit rank", "Allows user can edit the exists rank",
            TitleResName = "SA_EditRank",
            DescResName = "SA_EditRankDesc",
            PermssionSetResName = "SA_Community"
            )]
        public ActionResult Edit(int id)
        {
            return View(forumContext.Ranks.Find(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [SecurityAction("Community", "Edit rank", "Allows user can edit the exists rank",
            TitleResName = "SA_EditRank",
            DescResName = "SA_EditRankDesc",
            PermssionSetResName = "SA_Community"
            )]
        public ActionResult Edit(int id, FormCollection forms)
        {
            var rank = forumContext.Ranks.Find(id);
            if ((ModelState.IsValid) && rank!=null)
            {
                if (TryUpdateModel(rank,"Rank"))
                {
                    forumContext.Ranks.Update(rank);
                    if (rank.IsStart)
                    {
                        var exRanks = forumContext.Ranks.Filter(r => r.ID != rank.ID && r.IsStart).ToList();
                        foreach (var r in exRanks)
                        {
                            r.IsStart = false;
                            forumContext.Ranks.Update(r);
                        }
                    }
                    forumContext.SaveChanges();

                    _EnsureStartedRank();

                    return RedirectToAction("List");
                }
            }
            return View(rank);
        }

        private void _EnsureStartedRank()
        {
            if (forumContext.Ranks.Count(r => r.IsStart) == 0)
            {
                var firstRank = forumContext.Ranks.All().OrderBy(r => r.MinimumPosts).FirstOrDefault();
                if (firstRank != null)
                {
                    firstRank.IsStart = true;
                    forumContext.SaveChanges();
                }
            }
        }

        [HttpPost]
        [SecurityAction("Community", "Delete rank", "Allows user can delete the exists rank",
            TitleResName = "SA_DeleteRank",
            DescResName = "SA_DeleteRankDesc",
            PermssionSetResName = "SA_Community"
            )]
        public ActionResult Delete(int id)
        {
            var rank = forumContext.Ranks.Find(id);
            forumContext.Ranks.Delete(rank);
            forumContext.SaveChanges();
            _EnsureStartedRank();
            return RedirectToAction("List");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [SecurityAction("Community", "Create rank", "Allows user can create new rank",
            TitleResName = "SA_CreateRank",
            DescResName = "SA_CreateRankDesc",
            PermssionSetResName = "SA_Community"
            )]
        public ActionResult Create([Bind(Prefix = "Rank")] Rank rank)
        {
            if (ModelState.IsValid)
            {
                if (rank.IsStart)
                {
                    var exRanks = forumContext.Ranks.Filter(r => r.ID != rank.ID && r.IsStart).ToList();
                    foreach (var r in exRanks)
                    {
                        r.IsStart = false;
                        forumContext.Ranks.Update(r);
                    }
                }
                forumContext.Ranks.Create(rank);

                forumContext.SaveChanges();
                return RedirectToAction("List");
            }
            return View(rank);
        }
    }
}
